<?php

/**
 * 用户钱包信息. 
 */

namespace McUser\Controller;
use Common\Controller\HomebaseController;


class WalletController extends HomebaseController {

	function _initialize(){}
	

	/**
	 * 读出剩余分数.
	 * @return [type] [description]
	 */
	public function restMoney(){
		$userID = $_POST['userID'];
		// $token = $_POST['token'];

		/* 数据参数不能为空 */
		// if ( $userID == '' || $token == '' ){
		if ( $userID == '' ){
			$this->return_data_error('-2');
			return;
		}else{
			/* 检查 token 是否正确 */ 
			// if($this -> compare_token($userID, $token) === false){
			// 	return;
			// }
		}

		/*   现金余额   */
		$Wallet = D('Wallet');		
		$restMoney = $Wallet -> read_rest_money($userID);

		$data['userID'] = $userID;
		$data['restMoney'] = $restMoney;
			
		$this->return_data($data);
	}

	/*
	*	检查 Token
	*/
	public function CheckToken($userID, $token){
		/* 检查 token 是否正确 */ 
		$token_model = D('Token');
		$compare = $token_model -> compare_token_by_userID($userID, $token);
		if (! $compare){
			/* 手机号或者 Token 错误 */
			$rejson['retCode'] = "-111";
			
			$code = C('retJson');

			$rejson['retMsg'] = $code['-111'];
			$this -> ajaxReturn ($rejson) ;		
			return false;
		}
		return true;
	}

	/*  
	*	修改银行账号信息
	*/
	public function changeBankAccount(){
		$userID = $_POST['userID'];
		$bankName = $_POST['bankName'];
		$bankAccount = $_POST['bankAccount'];
		$bankUserName = $_POST['bankUserName'];
		$token = $_POST['token'];

		$code = C('retJson');

		/* 数据参数 ticker 不能为空 */
		if ( $userID == '' || $bankName == '' || $bankAccount == '' || $bankUserName == '' ){
			$rejson = array(
				"retCode" => "-2",
				"retMsg" => $code['-2'] );

			$this -> ajaxReturn ($rejson);
			return;
		}

		/*  检查 Token 的正确性  */
		$ctk = $this -> CheckToken($userID, $token);
		if (! $ctk)
			return;

		/* 检查 token 是否正确 */ 
		$token_model = D('Token');
		$compare = $token_model -> compare_token_by_userID($userID, $token);
		if (! $compare){
			/* 手机号或者 Token 错误 */
			$rejson['retCode'] = "-111";
			$rejson['retMsg'] = $code['-111'];
			$this -> ajaxReturn ($rejson) ;		
			return;
		}

		/*   先读本果指数的信息   */
		$bank = D('BgWalletBank');		
		$bank -> change_bank_account($userID,$bankName,$bankAccount,$bankUserName);

		$rejson = array(
				"retCode" => "1",
				"retMsg" => $code['1']
				);
			
		$this->ajaxReturn($rejson);
	}
 	
 	/*
 	*	读取银行账号信息
 	*/
	public function readBankAccount(){
		$userID = $_POST['userID'];
		$token = $_POST['token'];

		$code = C('retJson');

		/* 数据参数 ticker 不能为空 */
		if ( $userID == '' ){
			$rejson = array(
				"retCode" => "-2",
				"retMsg" => $code['-2'] );

			$this -> ajaxReturn ($rejson);
			return;
		}

		/*  检查 Token 的正确性  */
		$ctk = $this -> CheckToken($userID, $token);
		if (! $ctk)
			return;

		/*   先读本果指数的信息   */
		$bank = D('BgWalletBank');		
		$list = $bank -> read_bank_account($userID);

		$data['userID'] = $userID;
		$data['bankList'] = $list;

		$rejson = array(
				"retCode" => "1",
				"retMsg" => $code['1'] ,
				"data" => $data
				);
			
		$this->ajaxReturn($rejson);
	}

	/*
	*	读取奖金提成历史
	*/
	public function readBonusHis(){
		$userID = $_POST['userID'];
		$token = $_POST['token'];

		$code = C('retJson');

		/* 数据参数 ticker 不能为空 */
		if ( $userID == '' || $token == '' ){
			$rejson = array(
				"retCode" => "-2",
				"retMsg" => $code['-2'] );

			$this -> ajaxReturn ($rejson);
			return;
		}

		/*  检查 Token 的正确性  */
		$ctk = $this -> CheckToken($userID, $token);
		if (! $ctk)
			return;

		/*   先读本果指数的信息   */
		$wallet = D('BgWalletBonus');		
		$bonusList = $wallet -> read_bonus_his($userID);

		$data['userID'] = $userID;
		$data['bonusList'] = $bonusList;

		$rejson = array(
				"retCode" => "1",
				"retMsg" => $code['1'] ,
				"data" => $data
				);
			
		$this->ajaxReturn($rejson);
	}

	/*
	*	读取提现历史
	*/
	public function readTiXianHis(){
		$userID = $_POST['userID'];
		$token = $_POST['token'];

		$code = C('retJson');
		
		/* 数据参数 ticker 不能为空 */
		if ( $userID == '' || $token == '' ){
			$rejson = array(
				"retCode" => "-2",
				"retMsg" => $code['-2'] );

			$this -> ajaxReturn ($rejson);
			return;
		}

		/*  检查 Token 的正确性  */
		$ctk = $this -> CheckToken($userID, $token);
		if (! $ctk)
			return;

		/*   先读本果指数的信息   */
		$wallet = D('BgWalletTiXian');		
		$list = $wallet -> read_tixian_his($userID,'1');

		$data['userID'] = $userID;
		$data['txList'] = $list;

		$code = C('retJson');

		$rejson = array(
				"retCode" => "1",
				"retMsg" => $code['1'] ,
				"data" => $data
				);
			
		$this->ajaxReturn($rejson);
	}

	/*
	*	读取新的申请的提现列表
	*/
	public function readApplyListNew(){
		$userID = $_POST['userID'];
		$token = $_POST['token'];

		/* 数据参数 ticker 不能为空 */
		if ( $userID == '' ){
			
		}

		/*  检查 Token 的正确性  */
		$ctk = $this -> CheckToken($userID, $token);
		if (! $ctk)
			return;

		/*   先读本果指数的信息   */
		$wallet = D('BgWalletTiXian');		
		$list = $wallet -> read_tixian_his($userID,'0');

		$data['userID'] = $userID;
		$data['applyList'] = $list;

		$code = C('retJson');

		$rejson = array(
				"retCode" => "1",
				"retMsg" => $code['1'] ,
				"data" => $data
				);
			
		$this->ajaxReturn($rejson);
	}


	/*
	*	读取钱包信息
	*/
	public function readWalletInfo(){
		$userID = $_POST['userID'];
		$token = $_POST['token'];

		$code = C('retJson');

		/* 数据参数 ticker 不能为空 */
		if ( $userID == '' ){
			$rejson = array(
				"retCode" => "-2",
				"retMsg" => $code['-2'] );

			$this -> ajaxReturn ($rejson);
			return;
		}

		/*  检查 Token 的正确性  */
		$ctk = $this -> CheckToken($userID, $token);
		if (! $ctk)
			return;

		/*   先读本果指数的信息   */
		$wallet = D('BgWallet');		
		$data = $wallet -> read_wallet_info($userID);

		if (! $data){
			$sd = date('Y-m-d H:i:s', time());
			$data['userID'] = $userID;
			$data['bonusAll'] = 0;
			$data['cashYuE'] = 0;
			
			$data['lastBonus'] = 0;
			$data['lastBonusDate'] = $sd;
			$data['lastPayID'] = '';

			$data['tiXianAll'] = 0;
			$data['lastTiXian'] = 0;
			$data['lastTiXianDate'] = $sd;
		}

		$rejson = array(
				"retCode" => "1",
				"retMsg" => $code['1'] ,
				"data" => $data
				);
			
		$this->ajaxReturn($rejson);
	}

	/*
	*	申请提现
	*	1, 一个礼拜之内只能提现一次 
	*	2, 上次提现如果没有到账, 不能提现.
	*	3, 一次至少提现 100元.
	*/
	public function applyForCash(){
		$userID = $_POST['userID'];
		$applyCash = $_POST['applyCash'];
		$token = $_POST['token'];

		$code = C('retJson');

		/* 数据参数 ticker 不能为空 */
		if ( $userID == '' || $applyCash == '' ){
			$rejson = array(
				"retCode" => "-2",
				"retMsg" => $code['-2'] );

			$this -> ajaxReturn ($rejson);
			return;
		}

		/*  检查 Token 的正确性  */
		$ctk = $this -> CheckToken($userID, $token);
		if (! $ctk)
			return;

		/*   每次提现不能少于 100元   */
		if ( $applyCash < 100 ){
			$rejson = array(
				"retCode" => "-310",
				"retMsg" => $code['-310'] );

			$this -> ajaxReturn ($rejson);
			return;
		}		

		/*   检查奖金余额是否足够提现   */
		$wallet = D('BgWallet');		
		$data = $wallet -> read_wallet_info($userID);
		$yuE = $data['cashYuE'];
		if ( $applyCash > $yuE ){
			$rejson = array(
				"retCode" => "-303",
				"retMsg" => $code['-303'] );

			$this -> ajaxReturn ($rejson);
			return;
		}

		/*   先读本果指数的信息   */
		$tiXian = D('BgWalletTiXian');		

		/*  检查这次提现和上次提现是不是同一个礼拜   */
		$dd = $tiXian -> check_apply_valid($userID);		
		if ( $dd == '-301'){
			$rejson = array(
				"retCode" => "-301",
				"retMsg" => $code['-301'] );

			$this -> ajaxReturn ($rejson);
			return;
		}

		/*  检查上次提现有没有完成  */
		if ( $dd == '-302'){
			$rejson = array(
				"retCode" => "-302",
				"retMsg" => $code['-302'] );

			$this -> ajaxReturn ($rejson);
			return;
		}

		/*   没有问题, 添加提现操作   */
		$tiXian -> add_apply_for_cash($userID,$applyCash);

		$rejson = array(
				"retCode" => "1",
				"retMsg" => $code['1']
				);
			
		$this->ajaxReturn($rejson);
	}

	/*
	*	完成某一个提现申请
	*/
	public function completeApplyCash(){
		$userID = $_POST['userID'];
		$token = $_POST['token'];

		$code = C('retJson');

		/* 数据参数 ticker 不能为空 */
		if ( $userID == '' ){
			$rejson = array(
				"retCode" => "-2",
				"retMsg" => $code['-2'] );

			$this -> ajaxReturn ($rejson);
			return;
		}

		/*  检查 Token 的正确性  */
		$ctk = $this -> CheckToken($userID, $token);
		if (! $ctk)
			return;

		/*   修改提现标志 Flag   */
		$tiXian = D('BgWalletTiXian');		
		$d = $tiXian -> apply_cash_finished($userID);		

		/*   修改奖金余额   */
		$wallet = D('BgWallet');		
		$wallet -> apply_cash_finished($userID, $jinE, $tiXianID);

		$rejson = array(
				"retCode" => "1",
				"retMsg" => $code['1']
				);
			
		$this->ajaxReturn($rejson);
	}

}